package com.sishu.njrtsdms.jooq.listener;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.jooq.DSLContext;
import org.jooq.ExecuteContext;
import org.jooq.ExecuteListener;
import org.jooq.conf.Settings;
import org.jooq.impl.DSL;

@Slf4j
public class MyExecuteListener implements ExecuteListener {


    /**
     * 执行查询前,设置格式化渲染查询SQL
     *
     * @param ctx The context containing information about the execution.
     */
    @Override
    public void executeStart(ExecuteContext ctx) {

        // Create a new DSLContext for logging rendering purposes
        // This DSLContext doesn't need a connection, only the SQLDialect...
        DSLContext create = DSL.using(ctx.dialect(),

                // ... and the flag for pretty-printing
                new Settings().withRenderFormatted(true));

        // If we're executing a query
        if (ctx.query() != null) {
            log.info("----------------JOOQ Execute Normal Query Begin..........-------------------");
            log.info("formatted query :" + StrUtil.LF + "{}", create.renderInlined(ctx.query()));
        }

        // If we're executing a routine
        else if (ctx.routine() != null) {
            log.info("----------------JOOQ Execute Routine Begin..........-------------------");
            log.info("formatted query :" + StrUtil.LF + "{}", create.renderInlined(ctx.routine()));
        }
    }

}
